package com.netrust.quarz.task.utils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.netrust.quarz.common.enums.DeductPointEnumType;
import com.netrust.quarz.entity.HistoryAlarm;
import com.netrust.quarz.entity.RealAlarm;
import com.netrust.quarz.entity.Server;
import com.netrust.quarz.utils.DateUtils;
import com.netrust.quarz.utils.ProdUtils;

public class AlarmConverUtils {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List get(List list,List<Server> sis) {
		List<Map<String,String>> mapList=Lists.newArrayList();
		for (Object obj : list) {
			Map<String,String> map=JSON.parseObject(obj.toString(), Map.class);
			
			for (Server si : sis) {
				if (si.getIpAddr().equals(map.get("ip_addr"))) {
					map.put("depart",StringUtils.isNotBlank(si.getDept())?si.getDept():"-");
					map.put("operator",StringUtils.isNotBlank(si.getCarrier())?si.getCarrier():"-");
					map.put("serverName",StringUtils.isNotBlank(si.getServerName())?si.getServerName():"-");
				}
			}
			//设置级别
			map.put("slevel",level(map));
			
			mapList.add(map);
		}
		return mapList;
	}
	
	
	@SuppressWarnings("rawtypes")
	public List<RealAlarm> origin(List list,List<Server> sis) {
		List<RealAlarm> raList=Lists.newArrayList();
		for (Object obj : list) {
			@SuppressWarnings("unchecked")
			Map<String,String> map=JSON.parseObject(obj.toString(), Map.class);
			
			if(!ProdUtils.checkIp(map.get("ip_addr").toString())){
				continue;				
			}
			
			RealAlarm ra=new RealAlarm();
			 
			ra.setId(UUID.randomUUID().toString());
			ra.setAlarmTypeId(map.get("alarm_type_id"));
			ra.setName(map.get("name"));
			ra.setFirstCreate(StringUtils.isNotBlank(map.get("first_create_at"))?DateUtils.parseDate(map.get("first_create_at")):null);
			ra.setCause(map.get("cause"));
			ra.setAlarmId(map.get("alarm_id"));
			ra.setIpAddr(map.get("ip_addr"));
			ra.setClearTime(StringUtils.isNotBlank(map.get("clear_time"))?DateUtils.parseDate(map.get("clear_time")):null);
			ra.setClearUser(map.get("user_of_clear"));
			ra.setLocationInfo(map.get("location_info"));
			ra.setAdditional(map.get("additional"));
			ra.setActiveStatus(map.get("active_status"));
			ra.setClearStatus(map.get("clear_status"));
			ra.setAckStatus(map.get("ack_status"));
			ra.setOrigAlarmId(map.get("orig_alarm_id"));
			ra.setMoId(map.get("mo_id"));
			ra.setMoType(map.get("mo_type"));
			ra.setUpdateAt(new Date());
			ra.setCarrier(StringUtils.isNotBlank(map.get("ip_addr"))?ProdUtils.operator(map.get("ip_addr")):"");
			
			for (Server si : sis) {
				if (si.getIpAddr().equals(map.get("ip_addr"))) {
					ra.setDept(StringUtils.isNotBlank(si.getDept())?si.getDept():"-");
					ra.setCarrier(StringUtils.isNotBlank(si.getCarrier())?si.getCarrier():ProdUtils.operator(si.getIpAddr()));
					ra.setServerName(StringUtils.isNotBlank(si.getServerName())?si.getServerName():"-");
				}
			}
			
			//设置级别
			ra.setSeverity(level(map));
			
			raList.add(ra);
		}
		
		return raList;
	}
	
	@SuppressWarnings("rawtypes")
	public List originHistory(List list,List<Server> sis) {
		List<HistoryAlarm> raList=Lists.newArrayList();
		for (Object obj : list) {
			@SuppressWarnings("unchecked")
			Map<String,String> map=JSON.parseObject(obj.toString(), Map.class);
			
			if(!ProdUtils.checkIp(map.get("ip_addr").toString())){
				continue;				
			}
			
			HistoryAlarm ra=new HistoryAlarm();
			 
			ra.setAlarmTypeId(map.get("alarm_type_id"));
			ra.setName(map.get("name"));
			ra.setFirstCreate(StringUtils.isNotBlank(map.get("first_create_at"))?DateUtils.parseDate(map.get("first_create_at")):null);
			ra.setCause(map.get("cause"));
			ra.setAlarmId(map.get("alarm_id"));
			ra.setIpAddr(map.get("ip_addr"));
			ra.setClearTime(StringUtils.isNotBlank(map.get("clear_time"))?DateUtils.parseDate(map.get("clear_time")):null);
			ra.setClearUser(map.get("user_of_clear"));
			ra.setLocationInfo(map.get("location_info"));
			ra.setAdditional(map.get("additional"));
			ra.setActiveStatus(map.get("active_status"));
			ra.setClearStatus(map.get("clear_status"));
			ra.setAckStatus(map.get("ack_status"));
			ra.setOrigAlarmId(map.get("orig_alarm_id"));
			ra.setMoId(map.get("mo_id"));
			ra.setMoType(map.get("mo_type"));
			ra.setUpdateAt(new Date());
			ra.setCarrier(StringUtils.isNotBlank(map.get("ip_addr"))?ProdUtils.operator(map.get("ip_addr")):"");
			
			for (Server si : sis) {
				if (si.getIpAddr().equals(map.get("ip_addr"))) {
					ra.setDept(StringUtils.isNotBlank(si.getDept())?si.getDept():"-");
					ra.setCarrier(StringUtils.isNotBlank(si.getCarrier())?si.getCarrier():ProdUtils.operator(si.getIpAddr()));
					ra.setServerName(StringUtils.isNotBlank(si.getServerName())?si.getServerName():"-");
				}
			}
			
			//设置级别
			ra.setSeverity(level(map));
			
			raList.add(ra);
		}
		
		return raList;
	}
	
	private String level(Map<String, String> map){
		String severity=((map.get("severity"))!=null?map.get("severity"):"-");
		if (DeductPointEnumType.HINT.getIndex().equals(severity)) {
			return DeductPointEnumType.HINT.getName();
		}else if (DeductPointEnumType.SECONDARY.getIndex().equals(severity)) {
			return DeductPointEnumType.SECONDARY.getName();
		}else if (DeductPointEnumType.IMPORTANT.getIndex().equals(severity)) {
			return DeductPointEnumType.IMPORTANT.getName();
		}else if (DeductPointEnumType.IMERGENCY.getIndex().equals(severity)) {
			return DeductPointEnumType.IMERGENCY.getName();				
		}
		
		return "-";
	}
	


}
